<?php


namespace Module\AigcBase\Provider;


use ModStart\Core\Util\SerializeUtil;
use ModStart\Support\Concern\HasFields;

abstract class AbstractAigcProvider
{
    abstract public function name();

    abstract public function title();

    abstract public function type();

    public function help()
    {
        return '';
    }

    /**
     * 参数表单编辑
     * @param $builder HasFields
     * @param $param array
     * @return void
     */
    public function paramForm($builder, $param = [])
    {
        $builder->complexFields('param', '参数')
            ->fields([
                ['name' => 'key', 'title' => 'Key', 'type' => 'text', 'defaultValue' => '', 'tip' => '',]
            ])
            ->listable(false)->showable(false);
    }

    /** 参数显示
     * @param $item array
     * @param $param array
     * @return array
     * @returnExample
     * [
     *    {"name":"xxx","value":"xxx"}
     * ]
     */
    public function paramDisplay($item, $param = [])
    {
        $result = [];
        $itemParam = SerializeUtil::jsonDecode($item['param']);
        if (!empty($itemParam)) {
            foreach ($itemParam as $k => $v) {
                $result[] = [
                    'name' => $k,
                    'value' => $v,
                ];
            }
        }
        return $result;
    }
}
